package com.bitcola.exchange.script.util;

import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.bitcola.exchange.script.data.PlatformPrice;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 *  @author wx
 * @create 2019-03-19 17:06
 **/
public class PlatformPriceUtil {
    public static final String BINANCE = "http://47.242.16.156:8765/binance/api/v3/ticker/price";
    public static final String GATEIO = "http://47.242.16.156:8765/gateio/api2/1/tickers";
    public static final String BTC = "BTC";
    public static final String ETH = "ETH";
    public static final String USDT = "USDT";


    public static Map<String,BigDecimal> getBinancePrice(){
        String json = OKHttpUtil.httpGet(BINANCE);
        List<JSONObject> list = JSONObject.parseArray(json,JSONObject.class);
        List<PlatformPrice> result = new ArrayList<>();
        for (JSONObject object : list) {
            BigDecimal price = object.getBigDecimal("price");
            String symbol = object.getString("symbol");
            if (symbol.endsWith(BTC)){
                String coinCode = symbol.substring(0, symbol.length() - 3);
                result.add(new PlatformPrice(coinCode+"_"+BTC,price));
            } else if (symbol.endsWith(USDT)){
                String coinCode = symbol.substring(0, symbol.length() - 4);
                result.add(new PlatformPrice(coinCode+"_"+USDT,price));
            } else if (symbol.endsWith(ETH)){
                String coinCode = symbol.substring(0, symbol.length() - 3);
                result.add(new PlatformPrice(coinCode+"_"+ETH,price));
            }
        }
        Map<String,BigDecimal> map = new HashMap<>(600);
        for (PlatformPrice platformPrice : result) {
            map.put(platformPrice.getPair(),platformPrice.getPrice());
        }
        return map;
    }


    public static Map<String,BigDecimal> getGateioPrice(){
        String json = OKHttpUtil.httpGet(GATEIO);
        JSONObject object = JSONObject.parseObject(json);
        List<PlatformPrice> result = new ArrayList<>();
        if(object==null) return null;
        for (String pair : object.keySet()) {
            if (pair.endsWith(BTC.toLowerCase()) || pair.endsWith(USDT.toLowerCase()) || pair.endsWith(ETH.toLowerCase())){
                BigDecimal price = object.getJSONObject(pair).getBigDecimal("last");
                result.add(new PlatformPrice(pair.toUpperCase(),price));
            }
        }
        Map<String,BigDecimal> map = new HashMap<>(700);
        for (PlatformPrice platformPrice : result) {
            map.put(platformPrice.getPair(),platformPrice.getPrice());
        }
        return map;
    }



    public static String binanceKlinesUrl="http://support.huo-dun.com/binance/api/v3/klines";
    public static List<Number[]>queryKlines(String symbol,long start,long end,int limit){
        int maxLen=400;
        long min1=60*1000*5;
        List<Number[]> list = new ArrayList<>(600);
        symbol= symbol.replaceAll("_", "");

        do{

            long e=end>start+maxLen*min1?(start+maxLen*min1):end;
            String url=binanceKlinesUrl+"?"+String.format("symbol=%s&interval=5m&startTime=%d&endTime=%d",symbol,start,e);

            String json = OKHttpUtil.httpGet(url);

             var jsonArray= JSONArray.parseArray(json,ArrayList.class);
          var lists=   jsonArray.stream().map(x->{
                 var y=(List) x;

                 return new Number[] {Long.parseLong(y.get(0).toString()),Double.parseDouble(y.get(1).toString()),Double.parseDouble(y.get(2).toString()),Double.parseDouble(y.get(3).toString()),Double.parseDouble(y.get(4).toString()),Double.parseDouble(y.get(5).toString())};
             }).collect(Collectors.toList());
            start+=maxLen*min1;
            list.addAll(lists);

        }while (start<end);



        return list;



    }


    public static void main(String []s){

        queryKlines("BTC_USDT",1574245319000l,1574936519000l,1500);
    }



}
